﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Vega.USiteBuilder;
using SPCFMedicineInformer.DocumentTypes;
using umbraco.NodeFactory;
using SPCFMedicineInformer.Constants;
using Fonqi.Umbraco;
namespace SPCFMedicineInformer.Masterpages
{
    public partial class ContentGenericDetails : System.Web.UI.MasterPage
    {
        public string GenericName { get; set; }
        public string CategoryName { get; set; }
        public string GenId { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CategoryName = Request["catName"] != null ? Request["catName"].ToString() : "";
                GenericName = Request["genName"] != null ? Request["genName"].ToString() : "";
                int tempInt;
                int genId = int.TryParse(GenericName, out tempInt) ? tempInt : 0;
                int catId = int.TryParse(CategoryName, out tempInt) ? tempInt : 0;
                bool hasRecord = true;
                
                Node categoryNode = new Node(catId);
                if (categoryNode == null)
                {
                    hasRecord = false;
                }
                Node genericNameNode = new Node(genId);
                if (genericNameNode != null
                    && hasRecord
                    && !string.IsNullOrEmpty(genericNameNode.Name) 
                    && genericNameNode.Parent.Id == categoryNode.Id
                    && genericNameNode.NodeTypeAlias == "MedicineGenericName"
                    )
                {
                    CategoryName = categoryNode.Id.ToString();
                    GenericName = !string.IsNullOrEmpty(genericNameNode.GetPropertyAsString(UmbracoPropertyName.GENERIC_NAME_MT)) ? genericNameNode.GetPropertyAsString(UmbracoPropertyName.GENERIC_NAME_MT) : genericNameNode.Name;
                    GenId = genericNameNode.Id.ToString();

                    var MedicineList = NodeLocator.GetNodesOfExactType<MedicineItem>(genericNameNode.Id);
                    var node = ContentHelper.GetByNodeId<MedicineGenericName>(genericNameNode.Id); 
                    rptMedList.DataSource = MedicineList;
                    rptMedList.DataBind();
                    litDescription.Text = node.Description;
                    ShowContent(true);
                }
                else
                {
                    ShowContent(false);
                }
            }
            
        }
        void ShowContent(bool b)
        {
            plcContent.Visible = b;
            plcError.Visible = !b;
        }
    }
}